package com.lemon.smartsafetynew.ui.adapter.ledger;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingCheckItemVo;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingItemVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.patrol.CheckItemUtil;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/9/5.
 */

public class LedgerBuildingCheckItemListAdapter extends LemonRcvAdapter<LedgerBuildingCheckItemVo> {

    private RecyclerTClickListener<LedgerBuildingCheckItemVo> mListener;
    private LedgerBuildingItemVo ledgerBuildingItemVo;

    public LedgerBuildingCheckItemListAdapter(@Nullable List<LedgerBuildingCheckItemVo> data,
                                              RecyclerTClickListener<LedgerBuildingCheckItemVo> mListener,
                                              LedgerBuildingItemVo ledgerBuildingItemVo) {
        super(data);
        this.mListener = mListener;
        this.ledgerBuildingItemVo = ledgerBuildingItemVo;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ItemAdapter();
    }

    private class ItemAdapter implements AdapterItem<LedgerBuildingCheckItemVo> {

        private LedgerBuildingCheckItemVo mData;
        private int mPosition;

        private TextView itemNameTv;
        private TextView itemStatusTv;
        private TextView itemAbnormalCountTv;
        private TextView itemLocationTv;
        private LinearLayout itemLayout;

        @Override
        public int getLayoutResId() {
            return R.layout.item_ledger_check_item;
        }

        @Override
        public void bindViews(View root) {
            itemNameTv = (TextView)root.findViewById(R.id.item_name_tv);
            itemStatusTv = (TextView)root.findViewById(R.id.item_status_tv);
            itemAbnormalCountTv = (TextView)root.findViewById(R.id.item_abnormal_count_tv);
            itemLocationTv = (TextView)root.findViewById(R.id.item_location_tv);
            itemLayout = (LinearLayout)root.findViewById(R.id.item_layout);
        }

        @Override
        public void setViews() {
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);
                }
            });
        }

        @Override
        public void handleData(LedgerBuildingCheckItemVo ledgerBuildingCheckItemVo, int position) {

            mData = ledgerBuildingCheckItemVo;
            mPosition = position;

            if(position % 2 == 0) {
                itemLayout.setBackgroundResource(R.drawable.drawable_bg_common);
            } else {
                itemLayout.setBackgroundResource(R.drawable.drawable_label_bg_common);
            }

            itemNameTv.setText(CommonUtil.avoidNullString(ledgerBuildingCheckItemVo.getItem_name()));
            itemStatusTv.setText(CheckItemUtil.getCheckItemStatusString(ledgerBuildingCheckItemVo.getStatus()));
            itemStatusTv.setTextColor(Color.parseColor(CheckItemUtil.getCheckItemStatusColor(ledgerBuildingCheckItemVo.getStatus())));
            itemAbnormalCountTv.setText(CommonUtil.avoidNullString(ledgerBuildingCheckItemVo.getRed_num()));
            itemLocationTv.setText(CommonUtil.avoidNullString(ledgerBuildingItemVo.getBuilding_name())
                    + " " + CommonUtil.avoidNullString(ledgerBuildingCheckItemVo.getFloor_num())
                    + "楼 " +CommonUtil.avoidNullString(ledgerBuildingCheckItemVo.getPosition_mark()));
        }
    }
}
